Skip to content

feat: add Time type support to date_trunc function#19640

Merged
Jefffrey merged 6 commits intoapache:mainfrom
kumarUjjawal:feat/date_trunc_time_support
Jan 8, 2026
Merged

feat: add Time type support to date_trunc function#19640
Jefffrey merged 6 commits intoapache:mainfrom
kumarUjjawal:feat/date_trunc_time_support

Conversation

@kumarUjjawal
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • Added Time64/Time32 signatures to date_trunc
  • Added time truncation logic (hour, minute, second, millisecond, microsecond)
  • Error for invalid granularities (day, week, month, quarter, year)

Are these changes tested?

Yes

Are there any user-facing changes?

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Jan 5, 2026
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jan 5, 2026
kumarUjjawal and others added 3 commits January 5, 2026 14:16
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
_ => {
return exec_err!(
"second argument of `date_trunc` must be timestamp scalar or array"
"second argument of `date_trunc` must be timestamp, time scalar or array"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"second argument of `date_trunc` must be timestamp, time scalar or array"
"second argument of `date_trunc` must be a timestamp/time scalar or an array"

datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, None) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true Timestamp(Second, Some("+TZ")) SCALAR Truncates a timestamp value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true Date SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression)
datafusion public date_trunc datafusion public date_trunc FUNCTION true String SCALAR Truncates a timestamp or time value to a specified precision. date_trunc(precision, expression)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those return types correct - Date and String ? Shouldn't they be converted to Timestamp(Nanosecond, None)
I think the reason is here - https://github.com/apache/datafusion/pull/19640/changes#diff-7a6e17bfd876e9e88341d68750224b8a2294d9f401eef2126a49ade1665c336cR228

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these return types or input types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SELECT arrow_typeof(date_trunc('day', to_date('2024-01-15')));
-- Returns: Timestamp(ns)

SELECT arrow_typeof(date_trunc('day', '2024-01-15'));  
-- Returns: Timestamp(ns)

Yes, the actual runtime return type is Timestamp(Nanosecond), not Date or String.

The coercion from Date / String to Timestamp happens at planning time, before invoke_with_args is called.

The Date / String entries in information_schema represent the signature variants generated by the coercible API’s implicit coercion sources. However, the return_type method receives the already-coerced type (Timestamp) and returns that.

So the functional behavior is correct, this is how coercible signatures are represented in metadata.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks!

@Jefffrey Jefffrey added this pull request to the merge queue Jan 8, 2026
Merged via the queue into apache:main with commit 646213e Jan 8, 2026
29 checks passed
@Jefffrey
Copy link
Contributor

Jefffrey commented Jan 8, 2026

Thanks @kumarUjjawal & @martin-g

de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Part of apache#19025.

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

- Added Time64/Time32 signatures to date_trunc
- Added time truncation logic (hour, minute, second, millisecond,
microsecond)
- Error for invalid granularities (day, week, month, quarter, year)

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

Yes

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

---------

Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants